import { Service } from 'egg';

export default class UserService extends Service {
  // 查找所有
  async selectAll() {
    const all = await this.app.mysql.query(`select user.id, user.nickname, user.username, user.password, role.description as role 
    from user , role where user.role = role.rolename`)
    return all as Array<Object>;
  }

  // 根据username查找
  async selectByUsername(username: string) {
    const res = await this.app.mysql.select('user', {
      where: { username }
    })
    return res
  }

  // 添加一个
  async addOne(user) {
    const add = await this.app.mysql.insert('user', user);
    return add;
  }

  // 删除pet
  async delById(id) {
    const del = await this.app.mysql.delete('user', {
      id
    })
    return del
  }

  // 修改pet
  async updateById(user) {
    const update = await this.app.mysql.update('user', {
      ...user
    });
    return update
  }
}
